﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;

namespace WebSys.UI.OfficeFlow
{
    public class WorkFlowFiles : Page
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        Literal Literal1;
        //流程ID 
        public string iid
        {
            get
            {
                return Request["IID"];
            }
        }
        //岗位ID
        public string wiid
        {
            get
            {
                return Request["wiid"];
            }
        }
        //岗位名称
        public string step
        {
            get
            {
                return _FlowFactoryProvider.GetStepByWiid(wiid); ;
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            InitControl();
            if (!IsPostBack)
            {
                GetTreeHTML();
                //判断附件权限
                string sql = String.Format("SELECT AAR FROM ST_STEP T1,ST_INSTANCE T2 WHERE T1.WID=T2.WID AND T2.IID='{0}' AND T1.SNAME='{1}'", iid, step);
                string tt = _FlowFactoryProvider.GetStr(sql);
                if (!String.IsNullOrEmpty(tt))
                {
                    int Flag = Convert.ToInt32(tt);
                    if (Flag == 0)
                    {
                        System.Web.UI.HtmlControls.HtmlForm form1 = (System.Web.UI.HtmlControls.HtmlForm)FindControl("form1");
                        form1.Visible = false;
                        Response.Write("没有附件添加权限！请联系管理员！");
                        //没有权限
                        //Page.ClientScript.RegisterStartupScript(this.GetType(), "showordis", "<script>showordis();</script>");
                    }
                }
                
            }

        }
        protected void InitControl()
        {
            Literal1 = (Literal)FindControl("Literal1");
        }
        
        /// <summary>
        /// 获取目录
        /// </summary>
        /// <returns></returns>
        public void GetTreeHTML()
        {
            string sql = "select stepgroup from st_work_item where wiid='" + wiid + "'";
            string value = _FlowFactoryProvider.GetStr("select stepgroup from st_work_item where wiid=" + wiid);
            if (String.IsNullOrEmpty(value))
                value = step;
            DataTable dt = new DataTable();
            if ("-1".Equals(wiid))//查看---案件查询
            { dt = _FlowFactoryProvider.ListInstanceResource(iid); }
            else
            { dt = _FlowFactoryProvider.ListInstanceStepResource(wiid.ToString(), value, iid.ToString()); }
            dt.Columns.Add(new DataColumn("ParentId"));
            dt.Columns.Add(new DataColumn("Site"));
            dt.Columns.Add(new DataColumn("Name"));
            DataView dv = dt.DefaultView;
            dv.Sort = "Path";
            for (int i = 0; i < dv.Count; i++)
            {
                string path = dv[i]["Path"].ToString();
                string[] tmps = path.Split('/');
                dv[i]["Site"] = i + 1;
                if (tmps.Length > 1)//不是顶节点
                {
                    dv[i]["Name"] = tmps[tmps.Length - 1];
                    string pre = path.Substring(0, path.LastIndexOf('/'));
                    DataRow[] drs = dt.Select("Path='" + pre + "'");
                    if (drs.Length > 0)
                        dv[i]["ParentId"] = drs[0]["Site"].ToString();
                }
                else
                {
                    dv[i]["Name"] = dv[i]["Path"].ToString();
                    dv[i]["ParentId"] = 0;
                }
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("<select name='path' id='path' class='select' onchange='getval(this)' >\n");
            sb.Append("<option value='/'>/</option>\n");
            if ("-1".Equals(wiid))
            { printSubNodes(dt, sb, ""); }
            else
            {
                string wid = _FlowFactoryProvider.GetWidByIID(iid);
                printSubNodes(dt, sb, wid);
            }
            sb.Append("</select>\n");
            Literal1.Text = sb.ToString();
        }
        /// <summary>
        /// 查找子节点
        /// </summary>
        /// <param name="dt">树形结构数据集</param>
        /// <param name="sb">返回字符串</param>
        /// <param name="wid">岗位ID</param>
        private void printSubNodes(DataTable dt, StringBuilder sb, string wid)
        {
            foreach (DataRow dr in dt.Rows)
            {
                string type = dr["type"].ToString();
                String strOutput = string.Empty;
                DataRow[] drs = dt.Select("ParentId='" + dr["Site"].ToString() + "'");
                string sql = @"select id from st_static_resource where rtid='" + dr["rtid"].ToString() + "' and wid ='" + wid + "' and path='" + dr["Path"].ToString() + "'";
                string strssid = _FlowFactoryProvider.GetStr(sql);
                if (drs.Length > 0)//有子节点，说明不是叶结点
                {
                    strOutput = "<option value=\"" + strssid + "\">" + dr["Name"].ToString() + "</option>";
                }
                else
                {
                    if (type == "0")//目录
                    {
                        strOutput = "<option value=\"" + strssid + "\">" + dr["Name"].ToString() + "</option>";
                    }
                }
                sb.Append(strOutput);
            }
        }

    }
}


